#define DEBUG
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxn=5000;

int gcd(int a, int b) {
  for (int r=a%b; r; a=b, b=r, r=a%b);
  return b;
}

int main() {
  freopen("algebraic.in", "r", stdin);
  freopen("algebraic.out", "w", stdout);

  static ll a[maxn+1];
  int n;
  scanf("%d", &n);
  for (int i=1; i<=n; i++) {
    scanf("%lld", a+i);
  }

  sort(a+1, a+n+1);

  int m;
  scanf("%d", &m);
  for (int i=1; i<=m; i++) {
    int k, t;
    scanf("%d", &k);
    long long ans=0;
    if (k==0) {
      for (int i=1; i<=n; i++) {
	ans += a[i]*a[i];
      }
    } else {
      t=n/gcd(n, k);
      for (int j=0; j<n; j+=t) {
	ans += a[j+1]*a[j+2];
	if (t>2) {
	  ans += a[j+t]*a[j+t-1]+a[j+t]*a[j+t-2];
	  for (int x=3; x<=t-1; x+=2) {
	    ans += a[j+x]*a[j+x-2];
	  }
	  for (int x=4; x<=t-1; x+=2) {
	    ans += a[j+x]*a[j+x-2];
	  }
	} else {
	  ans += a[j+1]*a[j+2];
	}
      }
    }
    printf("%lld\n", ans);
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
